package com.whmskj.xjlhsc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whmskj.xjlhsc.entity.WbPlan;
import com.whmskj.xjlhsc.jitapi.entity.InventoryRecord;
import com.whmskj.xjlhsc.jitapi.mapper.InventoryMapper;
import com.whmskj.xjlhsc.mapper.WbPlanDetalMapper;
import com.whmskj.xjlhsc.entity.WbPlanDetal;
import com.whmskj.xjlhsc.service.WbPlanDetalService;
import com.whmskj.xjlhsc.service.WbPlanService;
import com.whmskj.xjlhsc.utils.SCUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lk
 * @since 2025-06-10 14:48:04
 */
@Service
@Slf4j
public class WbPlanDetalServiceImpl extends ServiceImpl<WbPlanDetalMapper, WbPlanDetal> implements WbPlanDetalService {

    @Autowired
    private WbPlanDetalMapper wbPlanDetalMapper;
    @Autowired
    @Lazy
    private WbPlanService wbPlanService;
    @Autowired
    private InventoryMapper inventoryMapper;

    @Override
    public Page<WbPlanDetal> findPage(WbPlanDetal params) {
        LambdaQueryWrapper<WbPlanDetal> query = Wrappers.lambdaQuery(WbPlanDetal.class);
        return wbPlanDetalMapper.selectPage(SCUtil.getPage(), query);
    }

    @Override
    public List<WbPlanDetal> findList(WbPlanDetal params) {
        LambdaQueryWrapper<WbPlanDetal> query = Wrappers.lambdaQuery(WbPlanDetal.class);
        return wbPlanDetalMapper.selectList(query);
    }

    @Override
    public WbPlanDetal findById(Long id) {
        return wbPlanDetalMapper.selectById(id);
    }

    @Override
    public boolean insert(WbPlanDetal wbPlanDetal) {
        return save(wbPlanDetal);
    }

    @Override
    public boolean update(WbPlanDetal wbPlanDetal) {
        return updateById(wbPlanDetal);
    }

    @Override
    public int delete(Long id) {
        return wbPlanDetalMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void copyGeneratorNext(Long maintenancePlanId, Long targetId) {
        LambdaQueryWrapper<WbPlanDetal> qe = Wrappers.lambdaQuery();
        qe.eq(WbPlanDetal::getWbPlanId, maintenancePlanId);
        List<WbPlanDetal> oldList = list(qe);

        CopyOptions copyOptions = new CopyOptions();
        copyOptions.setIgnoreProperties("detailId", "wbPlanId");
        List<WbPlanDetal> wbPlanDetals = BeanUtil.copyToList(oldList, WbPlanDetal.class, copyOptions);

        WbPlan wbPlan = wbPlanService.getById(maintenancePlanId);
        List<InventoryRecord> sourceInventMonth = inventoryMapper.getInventoryRecordByYm(wbPlan.getYmDate());
        Map<Integer, String> sourceMap = sourceInventMonth.stream().collect(Collectors.toMap(InventoryRecord::getRecordId, InventoryRecord::getDate));

        WbPlan targetPlan = wbPlanService.getById(targetId);
        List<InventoryRecord> targetInventMonth = inventoryMapper.getInventoryRecordByYm(targetPlan.getYmDate());
        if (CollectionUtil.isEmpty(targetInventMonth))
            throw new RuntimeException(targetPlan.getYmDate() + "未维护盘存月信息");

        Map<String, Integer> dateMapRecordId = targetInventMonth.stream().collect(Collectors.toMap(InventoryRecord::getDate, InventoryRecord::getRecordId));


        for (WbPlanDetal wbPlanDetal : wbPlanDetals) {
            wbPlanDetal.setWbPlanId(targetId);
            if (wbPlanDetal.getInventoryDateId() == 0) continue;
            String date = sourceMap.get(wbPlanDetal.getInventoryDateId());
            wbPlanDetal.setInventoryDateId(dateMapRecordId.get(LocalDate.parse(date).plusMonths(1).toString()));
        }
        saveBatch(wbPlanDetals);

    }
}